/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.dao.impl;

import java.util.List;

import com.eelpo.cmmp.core.spring.orm.jpa.BasicCommonDao;
import com.eelpo.cmmp.uac.dao.IRoleManageDao;
import com.eelpo.cmmp.uac.dao.entity.TRole;
import com.eelpo.cmmp.uac.dao.vo.Role;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.common.query.Expression;
import com.eelpo.common.query.JpaCriteria;
import com.eelpo.common.query.Order;
import com.eelpo.common.query.Result;
import com.eelpo.common.query.criteria.Criteria;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

/**
 * <pre>
 * 
 * 角色数据持久层具体实现
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Repository
public class RoleManageDao extends BasicCommonDao implements IRoleManageDao {

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IRoleManageDao#getRoleById(java.lang.String)
	 */
	@Override
	public TRole getRoleById(String id) throws DataAccessException {
		return find(TRole.class, id);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IRoleManageDao#checkExist(java.lang.String)
	 */
	@Override
	public boolean checkExist(String name) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TRole.class);
		criteria.result(Result.count());
		criteria.add(Expression.eq("name", name));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IRoleManageDao#checkExistByUpdate(java.lang.String
	 *      , java.lang.String)
	 */
	@Override
	public boolean checkExistByUpdate(String id, String name) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TRole.class);
		criteria.result(Result.count());
		criteria.add(Expression.ne("id", id));
		criteria.add(Expression.eq("name", name));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IRoleManageDao#checkRoleUsed(java.lang.String)
	 */
	@Override
	public boolean checkRoleUsed(String id) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TRole.class,"role");
		criteria.result(Result.count());
		criteria.add(Expression.and(Expression.isNotEmpty("users"), Expression.isNotEmpty("permissions")));
		criteria.add(Expression.eq("id", id));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IRoleManageDao#addRole(com.eelpo.cmmp.uac.dao
	 *      .entity.TRole)
	 */
	@Override
	public String addRole(TRole role) throws DataAccessException {
		persist(role);
		return role.getId();
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IRoleManageDao#updateRole(com.eelpo.cmmp.uac
	 *      .dao.entity.TRole)
	 */
	@Override
	public void updateRole(TRole role) throws DataAccessException {
		merge(role);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IRoleManageDao#deleteRole(java.lang.String)
	 */
	@Override
	public void deleteRole(String id) throws DataAccessException {
		remove(getReference(TRole.class, id));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IRoleManageDao#getRolesPaing(com.eelpo.cmmp.
	 *      uac.dao.vo.Role, int, int)
	 */
	@Override
	public List<TRole> getRolesPaing(Role role, int first, int max) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TRole.class);
		criteria.add(Expression.like("name", role.getName()));
		criteria.add(Expression.like("description", role.getDescription()));
		criteria.add(Expression.eq("status", Status.valueOf(role.getStatus())));
		criteria.order(Order.order(role.getSort()));
		criteria.setFirstResult(first);
		criteria.setMaxResults(max);
		return getResultList(criteria);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IRoleManageDao#getRolesPaing(java.lang.String,
	 *      com.eelpo.cmmp.uac.dao.vo.Role, int, int)
	 */
	@Override
	public List<TRole> getRolesPaing(String userId, Role role, int first, int max) throws DataAccessException {
		return null;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IRoleManageDao#getRolesCount(com.eelpo.cmmp.
	 *      uac.dao.vo.Role)
	 */
	@Override
	public int getRolesCount(Role role) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TRole.class);
		criteria.result(Result.count());
		criteria.add(Expression.like("name", role.getName()));
		criteria.add(Expression.like("description", role.getDescription()));
		criteria.add(Expression.eq("status", Status.valueOf(role.getStatus())));
		return getResultCount(criteria);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IRoleManageDao#getRolesCount(java.lang.String,
	 *      com.eelpo.cmmp.uac.dao.vo.Role)
	 */
	@Override
	public int getRolesCount(String userId, Role role) throws DataAccessException {
		return 0;
	}

}
